<template>
  <div class="add-scheme">
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="param"
      label-width="120px"
      class="dialog">
      <el-row >
        <el-col :span="12" :offset="6">
          <el-form-item label="共享方案编号" prop="shareSchemeNo">
            <el-input v-model="param.shareSchemeNo" placeholder="请输入共享方案编号" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" :offset="6">
          <el-form-item label="共享方案名称" prop="name">
            <el-input v-model="param.name" placeholder="请输入共享方案名称" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" :offset="6">
          <el-form-item label="共享方案类型">
            <el-select v-model="param.shareSchemeType" placeholder="请选择共享方案类型" clearable @change="changeSchemeType" style="width:100%">
              <el-option
                v-for="(item, index) in schemeTypelist"
                :key="index"
                :label="item.dicValue"
                :value="item.dicCode">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item align="center">
        <el-button type="primary" @click="onSave">确认</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
const DEFAULT_DATA = {
  name: '',
  shareSchemeNo: '',
  shareSchemeType: '',
  shareSchemeTypeName: ''
}
export default {
  data () {
    let checkSchemeNo = (rule, value, callback) => {
      if (this.type == 'edit' && value == this.editParam.shareSchemeNo) {
        callback()
      } else {
        this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/shareScheme/checkUnique`, {schemeNo: value}).then((res) => {
          if (res.result) {
            if (!res.data) {
              callback(new Error('共享方案编号已存在，请重新输入'))
            } else {
              callback()
            }
          } else {
            callback()
          }
        })
      }
    }
    return {
      param: Object.assign({}, DEFAULT_DATA),
      editParam: {},
      type: '',
      id: '',
      schemeTypelist: [],
      rules: {
        shareSchemeNo: [
          { required: true, message: '共享方案编号不允许为空', trigger: 'change' },
          { validator: checkSchemeNo, trigger: 'change' }
        ],
        name: [
          { required: true, message: '共享方案名称不允许为空', trigger: 'change' }
        ]
      }
    }
  },
  mounted () {
    this.getConfig()
    let path = this.$route.path.split('/')
    if (path[3] === 'editShareScheme') {
      this.type = 'edit'
      this.id = this.$route.params.id
      this.getInfo()
    } else {
      this.type = 'add'
    }
  },
  methods: {
    getConfig () {
      this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/dictionary/findDictionaryByType`, {
        type: 'main_type'
      }).then(res => {
        if (res.result) {
          this.schemeTypelist = res.data
        }
      }).catch(() => {})
    },
    getInfo () {
      this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/shareScheme/getShareSchemeDetail`, { code: this.id }).then(res => {
        if (res.result) {
          this.param = Object.assign({}, res.data, {
            shareSchemeTypeName: this.schemeTypelist[res.data.shareSchemeType]
          })
          this.editParam = Object.assign({}, this.param)
        }
      }).catch(() => {})
    },
    changeSchemeType (item) {
      this.param.shareSchemeTypeName = this.schemeTypelist[item]
    },
    onSave () {
      this.$refs['ruleForm'].validate(valid => {
        console.log('beforevalid')
        if (valid) {
          console.log('aftervalid')
          let data = {
            operateType: this.type === 'add' ? 'save' : 'edit',
            shareScheme: Object.assign({}, this.param)
          }
          if (this.type === 'edit') {
            data.shareScheme.code = this.param.code
          }
          this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/shareScheme/operationData`, data).then(res => {
            if (res.result) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.$router.push('/systemSettings/shareScheme/list')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(err => {
            this.$message.error(err)
          })
        }
      })
    },
    onCancel () {
      this.$router.push('/systemSettings/shareScheme/list')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-scheme {
  background: #fff;
  padding: 20px;
}
</style>

